package com.xbongbong.util;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/05/16 15:48
 */
public class SaasLinkProductUtil {

    /**
     * 列表页关联产品字段关联显示字段
     * @param explainList
     */
    public static void productLinkShowField(List<FieldAttrEntity> explainList) {
        for (FieldAttrEntity fieldAttrEntity : explainList) {
            if (Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.SELECT_PRODUCT.getType())){
                int index = 0;
                int productIndex = -1;
                List<FieldAttrEntity> keyList = new ArrayList<>();
                List<FieldAttrEntity> subField = (List<FieldAttrEntity>) fieldAttrEntity.getSubForm().getItems();
                for (FieldAttrEntity productField  : subField) {
                    if (Objects.equals(productField.getAttr(),SelectProductEnum.PRODUCT.getAttr())) {
                        for (LinkedAttrPoJo linkedAttrPoJo : productField.getLinkInfo().getLinkedShowAttr()) {
                            keyList.add(SaasLinkProductUtil.linkPojoToFieldList(linkedAttrPoJo));
                        }
                        productIndex = index;
                        break;
                    }
                    index++;
                }
                if (productIndex >= 0){
                    subField.addAll(productIndex + 1,keyList);
                }
                fieldAttrEntity.getSubForm().setItems(subField);
            }
        }
    }


    public static List<FieldAttrEntity> listProductLinkKeyField(FieldAttrEntity fieldAttrEntity) {
        List<FieldAttrEntity> keyList = new ArrayList<>();
        if (Objects.isNull(fieldAttrEntity)){
            return keyList;
        }
        SubFormPoJo subForm = fieldAttrEntity.getSubForm();
        if (Objects.isNull(subForm) || Objects.isNull(subForm.getItems())) {
            return keyList;
        }
        for (Object obj : subForm.getItems()) {
            FieldAttrEntity productField = JSONObject.parseObject(JSONObject.toJSONString(obj),FieldAttrEntity.class);
            if (Objects.equals(productField.getAttr(), SelectProductEnum.PRODUCT.getAttr())){
                for (LinkedAttrPoJo linkedAttrPoJo : productField.getLinkInfo().getLinkedShowAttr()) {
                    keyList.add(linkPojoToFieldList(linkedAttrPoJo));
                }
                break;
            }
        }
        return keyList;
    }

    public static FieldAttrEntity linkPojoToFieldList(LinkedAttrPoJo linkedAttrPoJo) {
        FieldAttrEntity fieldAttrEntity = new FieldAttrEntity();
        //夹带私货
        fieldAttrEntity.setSaasAttr(linkedAttrPoJo.getAttr());
        fieldAttrEntity.setAttr(XbbRefTypeEnum.PRODUCT.getCode() + StringConstant.COMMA + linkedAttrPoJo.getAttr());
        fieldAttrEntity.setParentAttr(linkedAttrPoJo.getParentAttr());
        fieldAttrEntity.setAttrName(linkedAttrPoJo.getAttrName());
        fieldAttrEntity.setDateType(linkedAttrPoJo.getDateType());
        fieldAttrEntity.setFieldType(linkedAttrPoJo.getFieldType());
        fieldAttrEntity.setComboType(linkedAttrPoJo.getComboType());
        fieldAttrEntity.setEditable(linkedAttrPoJo.getEditable());
        fieldAttrEntity.setItems(linkedAttrPoJo.getItems());
        fieldAttrEntity.setSubForm(linkedAttrPoJo.getSubForm());
        fieldAttrEntity.setVisible(linkedAttrPoJo.getVisible());
        fieldAttrEntity.setTheWidth(linkedAttrPoJo.getTheWidth());
        fieldAttrEntity.setWidthAttr(linkedAttrPoJo.getWidthAttr());
        fieldAttrEntity.setFreezeCount(linkedAttrPoJo.getFreezeCount());
        fieldAttrEntity.setFixed(linkedAttrPoJo.getFixed());
        fieldAttrEntity.setAttrNameEn(linkedAttrPoJo.getAttrNameEn());
        return fieldAttrEntity;
    }
}
